// proxyOrder.js
const app = getApp()
const util = require("../../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    datetime: {
      startTime: util.formatdate(new Date((new Date().getFullYear()), (new Date().getMonth() - 6), (new Date().getDate()))),
      endTime: util.formatdate(new Date()),
      startDate: new Date().getTime(),
      endDate: new Date().getTime(),
      timeshow: false,
      currentDate: new Date().getTime(),//1574646440123
      minDate: new Date((new Date().getFullYear() - 1), 0, 1).getTime(),
      maxDate: new Date((new Date().getFullYear() + 2), 11, 31).getTime(),
      timeStatus: 0
    },
    search: {
      searchAddShow: false,
      BtnTxt: '添加店铺',
      searchTip: '可输入ID、SN、店名等',
      searchValue: '',
    },
    list: [],
    pagenum: 1,

  },
  // 列表
  incomeStatistics: function () {
    var that = this;
    var data = { 
      page: that.data.pagenum,
      value: that.data.search.searchValue,
      start: that.data.datetime.startTime,
      end: that.data.datetime.endTime,
   };
   console.log(data)
    app.post('agent/allOrders', data).then((res) => {
      if (res.code == 200) {
        var list = that.data.list;
        var dataArr = res.list;
        list = list.concat(dataArr)
        that.setData({
          list: list
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.incomeStatistics();  
  },
  searchChange: function (event) {
    this.setData({
      ['search.searchValue']: event.detail
    })
  },
  // 搜索
  onSearch: function () {
    this.setData({
      list: [],
      pagenum: 1
    })
    this.incomeStatistics();
  },
  // 确认时间
  timeConfirm(event) {
    var that = this;
    var date = new Date(event.detail);
    var time = util.formatdate(date);
    var datetime = that.data.datetime;
    var status = datetime.timeStatus;
    if (status == 0) {
      that.setData({
        ['datetime.startTime']: time,
        ['datetime.startDate']: event.detail,
        ['datetime.timeshow']: false,
        ['datetime.minDate']: event.detail,
      });
      if (datetime.startDate > datetime.endDate) {
        that.setData({
          ['datetime.endTime']: time,
          ['datetime.endDate']: event.detail,
        });
      }
    } else if (status == 1) {
      that.setData({
        ['datetime.endTime']: time,
        ['datetime.endDate']: event.detail,
        ['datetime.timeshow']: false
      });
    }
  },
  // 显示时间选择
  timeShow(e) {
    var status = e.currentTarget.dataset.status;
    this.setData({
      ['datetime.timeshow']: true,
      ['datetime.timeStatus']: status,
    });
    if (status == 0) {
      this.setData({
        ['datetime.minDate']: new Date((new Date().getFullYear() - 1), 0, 1).getTime(),
      })
    }
  },
  // 关闭时间选择
  timeClose() {
    this.setData({ ['datetime.timeshow']: false });
  },
  // 退款
  refund:function(e){
    var that = this;
    var data = {
      id: e.currentTarget.dataset.id,
    };
    wx.showModal({
      title: '提示',
      content: '确定退款吗？',
      success(res){
        if(res.confirm){
          app.post('weixin/refundOrder', data).then((res) => {
            
            if (res.code == 200) {
              wx.showToast({
                title: '退款成功',
                icon: 'success',
                duration: 2000
              })
              that.setData({
                list: [],
                pagenum: 1
              })
              that.incomeStatistics();
            }
          }).catch((errMsg) => {
            console.log(errMsg);
            wx.showToast({
              title: "网络错误",
              icon: 'loading',
              duration: 2000
            })
          })
        }
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      pagenum: 1
    })
    this.incomeStatistics();
  },
  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom: function () {
    var that = this;
    var pagenum = that.data.pagenum + 1; //获取当前页数并+1
    that.setData({
      pagenum: pagenum, //更新当前页数
    })
    that.incomeStatistics();//重新调用请求获取下一页数据
  }
})